// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  traceUser: true,
  env:'aaa111-9gh9fp5ieac4de5d'
})

const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    if (event.newName) {
        console.log(event.newName)
        db.collection("trip").where({
            _id: event.tripId,
        }).update({
            data: {
                title: event.newName
            }
        }).then(res => {
            return true
        })
    }

    if (event.newBeginDate) {
        console.log(event.newBeginDate)
        db.collection('trip').where({
            _id: event.tripId
        }).get().then(res => {
            var tripBeginTime = res.data[0].tripBeginTime
            var tripEndTime = res.data[0].tripEndTime
            var delta = tripEndTime - tripBeginTime
            console.log(tripBeginTime, tripEndTime, delta)
            tripBeginTime = event.newBeginDate
            tripEndTime = tripBeginTime + delta
            db.collection('trip').where({
                _id: event.tripId
            }).update({
                data: {
                    tripBeginTime: tripBeginTime,
                    tripEndTime: tripEndTime
                }
            }).then(res => {
                return true
            })
        })
    }
    return false
}